part of '../widgets.dart';

class VanCellGroup extends StatelessWidget with VanProvide {
  /// props - 是否展示为圆角卡片风格(默认: false)
  final bool inset;

  /// props - 是否显示外边框(默认: true)
  final bool border;

  /// props - 样式
  final VanCellGroupStyle? style;

  /// props - 分组标题
  /// slots - 自定义分组标题
  final dynamic title;

  /// slots - 默认插槽
  final List<Widget>? children;

  const VanCellGroup({
    super.key,
    this.inset = false,
    this.border = true,
    this.style,
    this.title,
    this.children,
  });

  VanCellGroupStyle getComputedStyle(BuildContext context) {
    if (style?.isNonNull == true) return style!;
    VanBgColor? background = style?.background;
    Color? titleColor = style?.titleColor;
    EdgeInsets? titlePadding = style?.titlePadding;
    double? titleFontSize = style?.titleFontSize;
    double? titleLineHeight = style?.titleLineHeight;
    EdgeInsets? padding = style?.padding;
    Border? border = style?.border;
    BorderRadius? radius = style?.radius;

    background ??= kVanCellGroupBackground.get(context);
    titleColor ??= kVanCellGroupTitleColor.get(context);
    titleFontSize ??= kVanCellGroupTitleFontSize.get(context);
    titleLineHeight ??= kVanCellGroupTitleLineHeight.get(context);
    titlePadding ??= !inset ? kVanCellGroupTitlePadding.get(context) : kVanCellGroupInsetTitlePadding.get(context);
    padding ??= !inset ? null : kVanCellGroupInsetPadding.get(context);
    border ??= inset || !this.border
        //
        ? null
        : Border.symmetric(
            horizontal: BorderSide(
            color: kVanBorderColor.toColor(context) ?? Colors.transparent,
            width: 0.5,
          ));
    radius ??= !inset ? null : kVanCellGroupInsetRadius.get(context);
    return VanCellGroupStyle(
        background: background,
        titleColor: titleColor,
        titleFontSize: titleFontSize,
        titleLineHeight: titleLineHeight,
        titlePadding: titlePadding,
        padding: padding,
        border: border,
        radius: radius);
  }

  @override
  Widget build(BuildContext context) {
    final style = getComputedStyle(context);
    Widget? title0;
    if (title is Widget) {
      title0 = title;
    } else if (title is String) {
      if (title?.isNotEmpty == true) title0 = Text(title!);
    } else if (title != null) {
      _logger.e("VanCellGroup.title only type support [String] or [Widget].");
    }

    if (title0 != null) {
      title0 = DefaultTextStyle(
        style: TextStyle(
          fontSize: style.titleFontSize,
          height: calcLineHeight(
            lineHeight: style.titleLineHeight,
            fontSize: style.titleFontSize,
          ),
          color: style.titleColor,
        ),
        child: Container(
          padding: style.titlePadding ?? EdgeInsets.zero,
          child: title0,
        ),
      );
    }

    List<Widget> children0 = children ?? [];


    Widget widget0 = Container(
      width: double.infinity,
      margin: style.padding,
      decoration: BoxDecoration(
          shape: BoxShape.rectangle,
          color: style.background?.color,
          gradient: style.background?.gradient,
          borderRadius: style.radius,
          border: !inset && border ? style.border : null),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: children0,
      ),
    );

    if (title0 != null) {
      widget0 = children0.isEmpty
          ? widget0
          : Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [title0, widget0],
            );
    }

    return VanProvider(
      provide: style,
      child: VanProvider(provide: this, child: widget0),
    );
  }
}
